﻿namespace Chain.Common
{
    using System;
    using System.Text.RegularExpressions;

    public class Validator
    {
        public static bool IsDateTime(string strln)
        {
            if (strln != null)
            {
                string pattern = @"[1-2]{1}[0-9]{3}((-|\/|\.){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/|\.){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$";
                if (Regex.IsMatch(strln, pattern))
                {
                    int length = -1;
                    int index = -1;
                    int num3 = -1;
                    if (-1 != (length = strln.IndexOf("-")))
                    {
                        index = strln.IndexOf("-", (int) (length + 1));
                        num3 = strln.IndexOf(":");
                    }
                    else
                    {
                        length = strln.IndexOf("/");
                        index = strln.IndexOf("/", (int) (length + 1));
                        num3 = strln.IndexOf(":");
                    }
                    if (-1 == index)
                    {
                        return true;
                    }
                    if (-1 == num3)
                    {
                        num3 = strln.Length + 3;
                    }
                    int num4 = Convert.ToInt32(strln.Substring(0, length));
                    int num5 = Convert.ToInt32(strln.Substring(length + 1, (index - length) - 1));
                    int num6 = Convert.ToInt32(strln.Substring(index + 1, (num3 - index) - 4));
                    if (((num5 < 8) && (1 == (num5 % 2))) || ((num5 > 8) && ((num5 % 2) == 0)))
                    {
                        if (num6 < 0x20)
                        {
                            return true;
                        }
                    }
                    else if (num5 != 2)
                    {
                        if (num6 < 0x1f)
                        {
                            return true;
                        }
                    }
                    else if (((num4 % 400) == 0) || (((num4 % 4) == 0) && (0 < (num4 % 100))))
                    {
                        if (num6 < 30)
                        {
                            return true;
                        }
                    }
                    else if (num6 < 0x1d)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool IsEmail(string strln)
        {
            return Regex.IsMatch(strln, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        public static bool IsFax(string strln)
        {
            return Regex.IsMatch(strln, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
        }

        public static bool IsIDCard(string strln)
        {
            if (strln.Length == 0x12)
            {
                return IsIDCard18(strln);
            }
            return ((strln.Length == 15) && IsIDCard15(strln));
        }

        public static bool IsIDCard15(string strln)
        {
            long result = 0L;
            if (!long.TryParse(strln, out result) || (result < Math.Pow(10.0, 14.0)))
            {
                return false;
            }
            string str = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (str.IndexOf(strln.Remove(2)) == -1)
            {
                return false;
            }
            string s = strln.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (!DateTime.TryParse(s, out time))
            {
                return false;
            }
            return true;
        }

        public static bool IsIDCard18(string strln)
        {
            long result = 0L;
            if ((!long.TryParse(strln.Remove(0x11), out result) || (result < Math.Pow(10.0, 16.0))) || !long.TryParse(strln.Replace('x', '0').Replace('X', '0'), out result))
            {
                return false;
            }
            string str = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (str.IndexOf(strln.Remove(2)) == -1)
            {
                return false;
            }
            string s = strln.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (!DateTime.TryParse(s, out time))
            {
                return false;
            }
            string[] strArray = "1,0,x,9,8,7,6,5,4,3,2".Split(new char[] { ',' });
            string[] strArray2 = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2".Split(new char[] { ',' });
            char[] chArray = strln.Remove(0x11).ToCharArray();
            int a = 0;
            for (int i = 0; i < 0x11; i++)
            {
                a += int.Parse(strArray2[i]) * int.Parse(chArray[i].ToString());
            }
            int num4 = -1;
            Math.DivRem(a, 11, out num4);
            if (strArray[num4] != strln.Substring(0x11, 1).ToLower())
            {
                return false;
            }
            return true;
        }

        public static bool IsMobile(string strln)
        {
            return Regex.IsMatch(strln, @"^1[0123456789]\d{9}$", RegexOptions.IgnoreCase);
        }

        public static bool IsMoney(string strln)
        {
            return Regex.IsMatch(strln, @"[0-9]+\.?[0-9]*$");
        }

        public static bool IsNumber(string strln)
        {
            return Regex.IsMatch(strln, @"^([0]|([1-9]+\d{0,}?))(.[\d]+)?$");
        }

        public static bool IsOnllyChinese(string strln)
        {
            return Regex.IsMatch(strln, @"^[\u4e00-\u9fa5]+$");
        }

        public static bool IsPhone(string strln)
        {
            return Regex.IsMatch(strln, @"(^(\d{2,4}[-_－—]?)?\d{3,8}([-_－—]?\d{3,8})?([-_－—]?\d{1,7})?$)|(^0?1[35]\d{9}$)");
        }
    }
}

